//
//  WXBPaymentCodeView.m
//  Gaia
//
//  Created by weixb on 16/9/18.
//  Copyright © 2016年 weixb. All rights reserved.
//

#import "WXBPaymentCodeView.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//#define  boxWidth (SCREEN_WIDTH -70)/6 //密码框的宽度

@implementation WXBPaymentCodeView
{
    CGFloat boxWidth;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];// 先调用父类的initWithFrame方法
    if (self)
    {
        ///  TF
        _TF = [[UITextField alloc]init];
        _TF.delegate = self;
        _TF.placeholder = @"请输入验证码";
        _TF.keyboardType=UIKeyboardTypeNumberPad;
        [_TF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_TF];
        
        ///  假的输入框
        _view_box = [[UIView alloc]init];
        [self configViewBorder:_view_box];
        [self addSubview:_view_box];
        _view_box2 = [[UIView alloc]init];
        [self configViewBorder:_view_box2];
        [self addSubview:_view_box2];
        _view_box3 = [[UIView alloc]init];
        [self configViewBorder:_view_box3];
        [self addSubview:_view_box3];
        _view_box4 = [[UIView alloc]init];
        [self configViewBorder:_view_box4];
        [self addSubview:_view_box4];
        
        ///   密码点
        _lable_point = [[UILabel alloc]init];
        [_lable_point.layer setCornerRadius:5];
        [_lable_point.layer setMasksToBounds:YES];
        _lable_point.backgroundColor = [UIColor blackColor];
        [_view_box addSubview:_lable_point];
        
        _lable_point2 = [[UILabel alloc]init];
        [_lable_point2.layer setCornerRadius:5];
        [_lable_point2.layer setMasksToBounds:YES];
        _lable_point2.backgroundColor = [UIColor blackColor];
        [_view_box2 addSubview:_lable_point2];
        
        
        _lable_point3 = [[UILabel alloc]init];
        [_lable_point3.layer setCornerRadius:5];
        [_lable_point3.layer setMasksToBounds:YES];
        _lable_point3.backgroundColor = [UIColor blackColor];
        [_view_box3 addSubview:_lable_point3];
        
        _lable_point4 = [[UILabel alloc]init];
        [_lable_point4.layer setCornerRadius:5];
        [_lable_point4.layer setMasksToBounds:YES];
        _lable_point4.backgroundColor = [UIColor blackColor];
        [_view_box4 addSubview:_lable_point4];
        
        _lable_point.hidden=YES;
        _lable_point2.hidden=YES;
        _lable_point3.hidden=YES;
        _lable_point4.hidden=YES;
        _lable_point5.hidden=YES;
        _lable_point6.hidden=YES;
    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    boxWidth = self.frame.size.height;
    _TF.frame = CGRectMake(0, 0, 0, boxWidth);
    
    CGFloat centerX = self.frame.size.width/2;
    _view_box.frame = CGRectMake(centerX - boxWidth * 2 - 30, 0, boxWidth, boxWidth);
    _view_box2.frame = CGRectMake(centerX- boxWidth - 10, _view_box.frame.origin.y, boxWidth, boxWidth);
    _view_box3.frame = CGRectMake(centerX + 10, _view_box.frame.origin.y, boxWidth, boxWidth);
    _view_box4.frame = CGRectMake(centerX + boxWidth + 30, _view_box.frame.origin.y, boxWidth, boxWidth);
    
    _lable_point.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
    _lable_point2.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
    _lable_point3.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
    _lable_point4.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
}
#pragma mark - private
- (void)configViewBorder:(UIView *)view {
    view.layer.cornerRadius = 5;
    view.layer.borderWidth = 1.0f;
    view.layer.borderColor = [UIColor lightGrayColor].CGColor;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickView)];
    [view addGestureRecognizer:gesture];
}
- (void)clickView {
    [_TF becomeFirstResponder];
}
#pragma mark - <UITextFieldDelegate>
- (void) textFieldDidChange:(id) sender
{
    
    UITextField *_field = (UITextField *)sender;
    
    switch (_field.text.length) {
        case 0:
        {
            _lable_point.hidden=YES;
            _lable_point2.hidden=YES;
            _lable_point3.hidden=YES;
            _lable_point4.hidden=YES;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
        }
            break;
        case 1:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=YES;
            _lable_point3.hidden=YES;
            _lable_point4.hidden=YES;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
        }
            break;
        case 2:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=YES;
            _lable_point4.hidden=YES;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
        }
            break;
        case 3:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=NO;
            _lable_point4.hidden=YES;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
        }
            break;
        case 4:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=NO;
            _lable_point4.hidden=NO;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
        }
            break;
        case 5:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=NO;
            _lable_point4.hidden=NO;
            _lable_point5.hidden=NO;
            _lable_point6.hidden=YES;
        }
            break;
        case 6:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=NO;
            _lable_point4.hidden=NO;
            _lable_point5.hidden=NO;
            _lable_point6.hidden=NO;
        }
            break;
            
        default:
            break;
    }
    
    
    if (_field.text.length==4)
    {
        if ([self.delegate respondsToSelector:@selector(finishedPaymentCode:)]) {
            [self.delegate finishedPaymentCode:_field.text];
        };
        
    }
    
}
- (void)clearContent {
    _TF.text = @"";
    _lable_point.hidden=YES;
    _lable_point2.hidden=YES;
    _lable_point3.hidden=YES;
    _lable_point4.hidden=YES;
    _lable_point5.hidden=YES;
    _lable_point6.hidden=YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@"\n"])
    {
        return YES;
    }
    // 输入长度限制
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (_TF == textField)
    {
        if ([toBeString length] > 4) {
            textField.text = [toBeString substringToIndex:4];
            return NO;
        }
    }
    return YES;
}
@end
